# 导包
import datetime
import unittest
from api.login import LoginAPI
from common.gen_pwd import generate_password



# 创建测试类
class TestLogin(unittest.TestCase):
    # 前置处理
    def setUp(self) -> None:
        self.login_api = LoginAPI()
        self.password = generate_password("929itheima.CN032@")

    # 后置处理
    # def tearDown(self) -> None:
    #     pass

    # 定义测试用例
    # case001登录成功
    def test01_case001(self):
        """调用登录接口进行登录"""
        print(self.password)
        response = self.login_api.login({"mobile":"13800000002", "password": self.password})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get("success"))
        self.assertEqual(10000, response.json().get("code"))
        self.assertIn("操作成功！", response.json().get("message"))

    # case002不输入手机号
    def test02_case002(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile":"13800000002", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case003不输入密码
    def test03_case003(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "13800000002", "password": ""})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case004手机号长度小于11位
    def test04_case004(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "138000", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case005手机号长度大于11位
    def test05_case005(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "138000000022", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case006手机号输入非数字
    def test06_case006(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "asdghjkiuyh", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case007输入未注册手机号
    def test07_case007(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "13800000012", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case008多参
    def test08_case008(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "13800000002", "password": self.password, "parameter": "hahaha"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.json().get("success"))
        self.assertEqual(10000, response.json().get("code"))
        self.assertIn("操作成功！", response.json().get("message"))

    # case009少参-缺少手机号
    def test09_case009(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case010少参-缺少密码
    def test10_case010(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case011无参
    def test11_case011(self):
        # 调用登录接口进行登录
        response = self.login_api.login({})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case012错误参数-手机号参数拼写错误
    def test12_case012(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobil": "13800000002", "password": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

    # case013错误参数-密码参数拼写错误
    def test13_case013(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "13800000002", "passwor": "123456"})
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
